package com.tsky.common;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

/**
 * 工具类：Action操作
 * @author: cainiao
 * @version 1.0 
 * @time: 2010-3-10 上午06:02:50
 */
public final class ActionUtil {
	
	// IO的BUFFER大小
	private static final int BUFFER_SIZE = 16 * 1024;

	/**
	 * Ajax数据写操作
	 * @author: cainiao
	 * @time: 2010-3-10 上午06:00:25
	 * @function: 
	 * @param ajaxData
	 */
	public static void ajaxWrite(Object ajaxData){
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("UTF-8");
		response.setContentType("html/txt");
		response.setHeader("Pragma", "no-cache");
		response.setHeader("Cache-Control", "no-cache, must-revalidate");
		response.setHeader("Pragma", "no-cache");
        try {
			response.getWriter().write(ajaxData==null?"":ajaxData.toString());
			response.getWriter().flush();
			response.getWriter().close();
		} catch (Exception e) {
			SysUtil.printStackTrace(e);
		}
	}
	
	/**
	 * 获取前台页面传人的参数
	 * @param param
	 * @return
	 */
	public static String getParameter(String param){
		return ServletActionContext.getRequest().getParameter(param);
	}
	
	/**
	 * 
	 * @author：cainiao
	 * @方法描述：获取参数的数组值
	 * @date: 2010-3-16
	 * @param param
	 * @return
	 */
	public static Object[] getParameterValues(String param){
		return ServletActionContext.getRequest().getParameterValues(param);
	}
	
	/**
	 * 获取内存中的属性值
	 * @author: cainiao
	 * @time: 2010-3-10 上午06:01:10
	 * @function: 
	 * @param name
	 * @return
	 */
	public static Object getAttribute(String name){
		return getSession().getAttribute(name);
	}
	
	/**
	 * save to EL (Request Scope)
	 * @param name
	 * @param value
	 */
	public static void setToEL(String name, Object value){
		ServletActionContext.getRequest().setAttribute(name, value);
	}
	
	/**
	 * save to EL (Session Scope)
	 * @param name
	 * @param value
	 */
	public static void setToSession(String name, Object value){
		getSession().setAttribute(name, value);
	}
	
	/**
	 * 清除Session中的值
	 * @author: cainiao
	 * @time: 2010-3-10 上午06:01:36
	 * @function: 
	 * @param name
	 */
	public static void clearSession(String name){
		getSession().removeAttribute(name);
	}
	
	/**
	 * 获取用户的IP地址
	 * @author: cainiao
	 * @time: 2010-3-10 上午06:01:58
	 * @function: 
	 * @return
	 */
	public static String getIP(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String ip = request.getHeader("x-forwarded-for");
		if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		return ip;
	}
	
	/**
	 * 获取Session
	 * @author: cainiao
	 * @time: 2010-3-10 上午06:02:23
	 * @function: 
	 * @return
	 */
	public static HttpSession getSession() {
		return ServletActionContext.getRequest().getSession();
	}
	
	/**
	 * 获取相应
	 * @author: cainiao
	 * @time: 2010-6-4 上午07:00:55
	 * @function: 
	 * @return
	 */
	public static HttpServletResponse getResponse(){
		return ServletActionContext.getResponse();
	}
	
	/**
	 * 获取请求
	 * @author: cainiao
	 * @time: 2010-6-13 上午03:44:39
	 * @function: 
	 * @return
	 */
	public static HttpServletRequest getRequest(){
		return ServletActionContext.getRequest();
	}
	
	/**
	 * 获取当前用户的角色ID
	 * @author: cainiao
	 * @time: 2010-3-10 上午06:02:32
	 * @function: 
	 * @return
	 */
	public static String getRoleID(){
		// 获取已登录对象信息
		Object loginUser = getSession().getAttribute("loginUser");
		String roleid = null;
		try {
			roleid = (String)loginUser.getClass().getMethod("getRoleId").invoke(loginUser);
		} catch (Exception e) {
			SysUtil.printStackTrace(e);
		} 
		return roleid;
	}
	
	/**
	 * 获取当前用户的ID
	 * @author: cainiao
	 * @time: 2010-3-10 上午06:02:10
	 * @function: 
	 * @return
	 */
	public static String getUserID(){
		// 获取已登录对象信息
		Object loginUser = getSession().getAttribute("loginUser");
		String userid = null;
		try {
			userid = (String)loginUser.getClass().getMethod("getId").invoke(loginUser);
		} catch (Exception e) {
			SysUtil.printStackTrace(e);
		} 
		return userid;
	}
	
	/**
	 * 获取当前用户的code
	 * @author: cainiao
	 * @time: 2010-3-10 上午06:02:10
	 * @function: 
	 * @return
	 */
	public static String getUserCode(){
		// 获取已登录对象信息
		Object loginUser = getSession().getAttribute("loginUser");
		String userid = null;
		try {
			userid = (String)loginUser.getClass().getMethod("getCode").invoke(loginUser);
		} catch (Exception e) {
			SysUtil.printStackTrace(e);
		} 
		return userid;
	}
	
	/**
	 * 获取当前用户的Name
	 * @author: cainiao
	 * @time: 2010-3-10 上午06:02:10
	 * @function: 
	 * @return
	 */
	public static String getUserName(){
		// 获取已登录对象信息
		Object loginUser = getSession().getAttribute("loginUser");
		String userid = null;
		try {
			userid = (String)loginUser.getClass().getMethod("getName").invoke(loginUser);
		} catch (Exception e) {
			SysUtil.printStackTrace(e);
		} 
		return userid;
	}
	
	/**
	 * 文件上传
	 * @author: cainiao
	 * @time: 2010-6-8 上午01:48:27
	 * @function: 
	 * @param src
	 * @param dst
	 */
	public static void upload(File src, String id, String fileName, String savePath) {
		// 文件后缀
		String fileExt = getFileExt(fileName);
		String dstPath = ServletActionContext.getServletContext().getRealPath(savePath)
				+ "\\" + id+ "." + fileExt;
		
		File dst = new File(dstPath);
		
		InputStream in = null;
		OutputStream out = null;
		try {
			in = new BufferedInputStream(new FileInputStream(src), BUFFER_SIZE);
			out = new BufferedOutputStream(new FileOutputStream(dst),
					BUFFER_SIZE);
			byte[] buffer = new byte[BUFFER_SIZE];
			int len = 0;
			while ((len = in.read(buffer)) > 0) {
				out.write(buffer, 0, len);
			}
		} catch (Exception e) {
			SysUtil.printStackTrace(e);
		} finally {
			if (null != in) {
				try {
					in.close();
				} catch (Exception e) {
					SysUtil.printStackTrace(e);
				}
			}
			if (null != out) {
				try {
					out.close();
				} catch (Exception e) {
					SysUtil.printStackTrace(e);
				}
			}
		}
	}
	
	/**
	 * 
	 * @author：cainiao
	 * @方法描述：获取文件目录
	 * @date: 2010-3-16
	 * @param savePath
	 * @return
	 */
	public static String getDir(String savePath){
		return ServletActionContext.getServletContext().getRealPath(savePath);
	}
	
	/**
	 * 文件下载
	 * @author: cainiao
	 * @time: 2010-6-10 上午07:33:37
	 * @function: 
	 * @param dir
	 * @return
	 */
	public static InputStream download(String dir) {
		return ServletActionContext.getServletContext().getResourceAsStream(dir);
	}
	
	/**
	 * 
	 * @author：cainiao
	 * @方法描述：获取文件后缀名
	 * @date: 2010-3-16
	 * @param fileName
	 * @return
	 */
	public static String getFileExt(String fileName) {
		return fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
	}
	
}
